import json

import requests

import loadInterData
from foodSqlDao import FoodDao
from faker import Faker

fake = Faker('zh_CN')

indexFileData = 0  # 当前读到第几个文件
fileNamePrefix = 'foodStoreData'  # 'foodStoreData'  # foodStoreDataVirtual 文件前缀--------

foodStoreData = []  # 读取一个文件所得到的foodData数据
urlItemSave = 'http://127.0.0.1:8080/api/admin/store/product/save'
urlCommentSave = 'http://127.0.0.1:8080/api/admin/store/product/reply/save'
# headers = {'Authorization': PEXELS_API_KEY}
headers = {
    'User-Agent': 'Mozilla/5.0 (X11; Linux aarch64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.188 Safari/537.36 CrKey/1.54.250320',
    'Authori-Zation': '78aaf3ade221406f8fa617678c2ec65c',
    'Content-Type': 'application/json'
}

for file_index in range(10, 11):  # 第一个索引值，看上次你读取到哪里的索引-------------- 6
    # 加载生成的食品数据
    print(f"加载{fileNamePrefix}{file_index}文件")
    with open(f'../foodJsonData/{fileNamePrefix}{file_index}.json', 'r', encoding='UTF-8') as f:
        foodStoreData = json.load(f)
        indexFileData=file_index
    while len(foodStoreData) > 0:
        storeProduct = foodStoreData.pop()  # pop一个data数据进行插入

        # storeProduct['id'] = itemID  # 指定ID 指定个狗屁
        # 爬取数据的时候把某个字段写错了 这里修改一下
        storeProduct['sliderImage']=json.dumps(storeProduct['sliderImages'])
        storeProduct["attrValue"][0]["stock"] = 99999

        # storeProduct['storeName'] = storeProduct['storeName'] + fake.word()  # 担心名称重复可以加入这个 对于后面随机生成的数据来讲--------------------------------

        # 将JSON数据转换为字符串
        storeProduct = json.dumps(storeProduct)
        # 插入Item-------------------
        response = requests.post(urlItemSave, data=storeProduct, headers=headers)
        if response.status_code == 200:
            data = response.json()
            # print(data)
            if data['code'] == 200:
                print(f"插入item成功")
            else:
                print(f"插入item失败，code!=200")
        else:
            print(f"插入item失败，系统无法响应")
    # 插入Item-------------------

print('++++++++++++++++++++++++++++++++')
print(f'当前读取到文件索引{indexFileData}')
re = {
    '数据文件前缀': fileNamePrefix,
    '当前读取到文件索引': indexFileData
}
# 数据库出错了，
with open('./re/insetResult_' + fileNamePrefix + '.json', 'w', encoding='utf-8') as f:
    json.dump(re, f, ensure_ascii=False, indent=4)


# 它大爷的 后面发现其实它的电影ID就是1-1682 所以干脆直接插入电影就好了，，压根不需要读取交互文件  操他吗的、日 浪费老子电脑何时间
